#include "hx711.h"

volatile int Meau_Indext = 0;
volatile int Kay_State = 1; //检测按键是否按下
volatile float fHusk = 0;//保存皮重
volatile int husk_flag = 1;
typedef struct meau{
    u8 enter;
    u8 back;
    u8 current;
    void(*pvfShowfun)(void);
}Meau_t;


Meau_t xMeau[4] = {
    {2,0,1,vShow1},
    {3,1,0,vShow2},
    {2,0,2,vShow1_1},
    {3,1,3,vShow2_1},

};



void vShow1(void)
{
    vDisplay_Clear();
    vDisplay_string(0,0,"Husk      <<<");
    vDisplay_string(0,1,"Not Husk  ");
}
void vShow2(void)
{
    vDisplay_Clear();
    vDisplay_string(0,0,"Husk      ");
    vDisplay_string(0,1,"Not Husk  <<<");
}

void vShow1_1(void)
{
    vDisplay_Clear();
    vDisplay_string(0,0,"Weight :");
    vDisplay_string(0,1,"g");
    vTest_Weight();
}

void vShow2_1(void)
{
    vDisplay_Clear();
    vDisplay_string(0,0,"Weight :");
    vDisplay_string(0,1,"g");
    vTest_Weight();
}


void vKeyInit(void)
{
    GPIO_InitTypeDef HX711_Key;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    HX711_Key.GPIO_Mode =   GPIO_Mode_IPU;
	HX711_Key.GPIO_Pin	= GPIO_Pin_2 | GPIO_Pin_6 |  GPIO_Pin_13 | GPIO_Pin_7;
	GPIO_Init(GPIOA,&HX711_Key);
}

void vMeau_Switch(void)
{
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) == Bit_RESET)//同级切换
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) == Bit_RESET);
        vDelayms(5);
        Meau_Indext = xMeau[Meau_Indext].current;
        Kay_State = 1;
    }

    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET)//确认键
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET);
        vDelayms(5);
        if(Meau_Indext == 2)
        {
            husk_flag = 1;
        }
        Meau_Indext = xMeau[Meau_Indext].enter;
        Kay_State = 1;
    }

    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == Bit_RESET)//返回键
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == Bit_RESET);
        vDelayms(5);
        Meau_Indext = xMeau[Meau_Indext].back;
        Kay_State = 1;
    }

    if(Kay_State && (Meau_Indext < 2))//检测是否有新按键按下，避免屏幕一直刷新
    {
        xMeau[Meau_Indext].pvfShowfun();  
        Kay_State = 0;
    }     
    if(Meau_Indext >= 2)
    {
        xMeau[Meau_Indext].pvfShowfun();  
        Kay_State = 0;
    }

}

void vTest_Weight(void)
{
    float fWeight = 0;
    if(Meau_Indext == 2)//去皮
    {
        if(husk_flag == 1)
        {
             fHusk = fGetHX711();
             husk_flag = 0;
             vDelayms(500);
        }
        fWeight = fGetHX711() - fHusk;
         if(fWeight < 0)
            fWeight = 0;
        vDisplay_floatnum(2, 1, fWeight);
    }

    if(Meau_Indext == 3)//不去皮
    {
        fWeight = fGetHX711();
        vDisplay_floatnum(2, 1, fWeight); 
    }
    vDelayms(2000);
}


void vHx711Init(void)
{
	GPIO_InitTypeDef HX711_gpio;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	HX711_gpio.GPIO_Mode = GPIO_Mode_Out_PP;
	HX711_gpio.GPIO_Pin	= GPIO_Pin_4;//SCK
	HX711_gpio.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&HX711_gpio);

	HX711_gpio.GPIO_Mode =   GPIO_Mode_IPU;
	HX711_gpio.GPIO_Pin	= GPIO_Pin_5;//DT
	GPIO_Init(GPIOA,&HX711_gpio);
    GPIO_ResetBits(GPIOA, GPIO_Pin_4);
    vDelayus(60);
}

float fGetHX711(void)
{
    u32 c = 0;
	int i = 0;
	while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == Bit_SET);
	for(i = 0; i < 24; i++)
	{
         vDelayus(50);
        GPIO_SetBits(GPIOA, GPIO_Pin_4);
		c = (c << 1);
		if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == Bit_SET)
			c += 1;
        vDelayus(10);
        GPIO_ResetBits(GPIOA, GPIO_Pin_4);
	}
	GPIO_SetBits(GPIOA, GPIO_Pin_4);
    vDelayus(5);
	GPIO_ResetBits(GPIOA, GPIO_Pin_4);
    c ^= 0x800000;
    c /=1000; 
    c = c-7889-492 ;
    if(c < 0)
        c = 0;
    i = (int) c; 
    return i;
}
